package com.zj.easytotwitter.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.viewpagerindicator.TabPageIndicator;
import com.zj.easytotwitter.MainActivty;
import com.zj.easytotwitter.R;
import com.zj.easytotwitter.ShareUtils;
import com.zj.easytotwitter.adapter.PolicyTabPageAdapter;
import com.zj.easytotwitter.event.DataEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZJ on 2018/4/22 0022.
 * 首页碎片
 */

public class HomePageFragment extends BaseFragment {
    View view;
    @BindView(R.id.indicator)//导航栏
    TabPageIndicator indicator;
    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private PolicyTabPageAdapter adapter;
    private List<String> list=new ArrayList<>();

    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        Context contextTheme = new ContextThemeWrapper(getActivity(),R.style.StyledIndicators);

        LayoutInflater themeInflater = inflater.cloneInContext(contextTheme);

        view = themeInflater.inflate(R.layout.fra_home_page, container, false);
        getLocalPis();
        return view;
        //return inflater.inflate(R.layout.fra_home_page, container, false);
    }

    @Override
    public void initData() {
        list.add("全部");
        list.add("排行");
        list.add("全网");
        list.add("美搭");
        list.add("日用");
        //初始化
        adapter = new PolicyTabPageAdapter(getFragmentManager(),list);
        viewPager.setAdapter(adapter);
        indicator.setViewPager(viewPager);
        indicator.setVisibility(View.VISIBLE);
    }

    @Subscribe
    public void onEventMainThread(DataEvent event) {
        if (event.getPos()==9999){
            ShareUtils.share9PicsToWXCircle(getActivity(), "今天心情非常好",paths);
        }
    }

    //下载获取本地图片
    private File[] files;
    private List<String> paths = new ArrayList<String>();
    private final static String FILENAME="/sdcard/Test/";
    private void getLocalPis(){
        //ImageView image =(ImageView)findViewById(R.id.image);
        String path= Environment.getExternalStorageDirectory().getAbsolutePath();

        // 遍历 SD 卡下 .png 文件通过微信分享，保证SD卡根目录下有.png的文件
//        File root = Environment.getExternalStorageDirectory();
        File root = new File(path+"/sdcard/");

//        File destDir = new File(FILENAME); if (!destDir.exists()) { destDir.mkdirs();  }

        Bitmap abitmap = new BitmapFactory().decodeResource(getResources(), R.drawable.header_default);
        Bitmap bbitmap = new BitmapFactory().decodeResource(getResources(), R.drawable.header_default);
        //image.setImageBitmap(abitmap);
        File b = new File(path+"/sdcard/");
        if(!b.exists()){
            b.mkdirs();
        }

        try {
            File a = new File(path+"/sdcard/", "a.png");
            File bmap = new File(path+"/sdcard/", "b.png");
            if (!a.exists()) {
                a.createNewFile();
            }
            if(!b.exists()){
                bmap.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(a);
            FileOutputStream bout = new FileOutputStream(bmap);
            abitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            abitmap.compress(Bitmap.CompressFormat.PNG, 90, bout);
            out.flush();
            out.close();
            bout.flush();
            bout.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        files = root.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                Toast.makeText(getActivity(), pathname.getName(), Toast.LENGTH_SHORT).show();
                if (pathname.getName().endsWith(".png"))
                    return true;
                return false;
            }
        });
        for(File file :files){
            paths.add(file.getAbsolutePath());
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
